<?php
namespace Home\Controller;

/*
 * ADN转化所需控制器
 */
class AdnController extends MainController {


    /*
     * Adn转换
     * newID ：Int : document表中的ID
     */
    public function adnchange($newID){
        $info = M('document as d') -> join(C('DB_PREFIX').'file as f') -> field('f.*') -> where("d.fileid=f.id and d.id='$newID'") -> find();
        $this -> convert($info['allpath'] , 'bim' , $newID);
    }


    //ADN 转化
    public function convert($path,$type,$id){
        $url		= 'http://'.$_SERVER['HTTP_HOST'].':61111';
        $headers	= array(
            'Content-Type: Content-type: application/x-www-form-urlencoded'
        );
        $field		= array(
            'file'		=>	$_SERVER['DOCUMENT_ROOT'] . (__ROOT__ ? __ROOT__.'/' : '') .$path,
            'download' 	=>	$_SERVER['DOCUMENT_ROOT']. (__ROOT__ ? __ROOT__.'/' : '') .'/Public/upload/model/',
            'host'		=>	$_SERVER['HTTP_HOST'],
            'path'		=>	U('Home/Adn/adnreturn?type='.$type.'&id='.base64($id))
        );
        $fields		= http_build_query($field);
        $cl			= curl_init();
        curl_setopt($cl, CURLOPT_URL, $url);
        curl_setopt($cl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($cl, CURLOPT_POST, true);
        curl_setopt($cl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($cl, CURLOPT_POSTFIELDS, $fields);
        $content = curl_exec($cl);
        curl_close($cl);
        return $content;
    }


    //ADN_return回路
    public function adnreturn($type='',$id=''){
        $id = base64($id,false);
        $post = file_get_contents('php://input');
        $res = json_decode($post,true);
        if($res['success'] == 'true'){
            $loadfiles = $res['loadFiles'][0];
            $adn_img = $res['thumbnailFiles'][0];

            //文件夹名称
            $fileinfo = M('document as d') -> join(C('DB_PREFIX').'file as f') -> field('f.*') -> where("d.fileid=f.id and d.id='$id'") -> find();
            $FolderName = substr($fileinfo['savename'],0,strpos($fileinfo['savename'],'.'));
            $prefolder = 'Public/upload/model/'.$FolderName;
            
            //改变相应参数.
            $save = array(
                'update' => time(),
                'convertstatus' => 3, //成功
                'adnimg' => $prefolder.$adn_img["path"], //adn_img
                'adnpath' => $prefolder.$loadfiles["path"], //adn_path
                'adntitle' => $loadfiles["title"], //adn_title
                'adnfolder' => $prefolder.$loadfiles["folder"], //ADN文件夹路径
                'adntype' => $loadfiles["type"] //ADN类型
            );
        }else{
            //改变相应参数.
            $save = array(
                'update' => time(),
                'convertstatus' => 4 //失败
            );
        }
        M('document') -> where(array('id'=>$id)) -> save($save);
    }


}
